package com.apchernykh.rozhlasapp.player;

import com.apchernykh.ceskyrozhlas.basic.R;
import com.apchernykh.rozhlasapp.ui.RozhlasAppBaseFragmentActivity;
import com.apchernykh.types.ApplicationModel.EPlaybackStatuses;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ToggleButton;

public class PlaybackControlView extends LinearLayout {

	public PlaybackControlView(Context iContext, AttributeSet iAttrs) {
		super(iContext, iAttrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.playback_control_view, this);

		this.findViewById(R.id.button_zastavit).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View iView) {
						RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService
								.stopPlayback();
					}
				});

		syncToPlaybackState();

		((ToggleButton) this.findViewById(R.id.button_pozadi))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						ToggleButton aButton = (ToggleButton) v;
						if (aButton != null) {
							if (aButton.isChecked()) {
								RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService
										.enableBackgroundPlayback();
							} else {
								RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService
										.disableBackgroundPlayback();
							}
						}
					}
				});

	}

	public void syncToPlaybackState() {
		ToggleButton aPozadiEnabledButton = (ToggleButton) this
				.findViewById(R.id.button_pozadi);
		if (aPozadiEnabledButton != null) {
			aPozadiEnabledButton
					.setChecked(RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService
							.isBackgroundEnabled());
		}
		int aGeneralVisibility = RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService
				.getPlaybackStatus() == EPlaybackStatuses.STOPPED ? View.GONE
				: View.VISIBLE;
		// aPlayButton.setVisibility(aTargetVisibility);
		setVisibility(aGeneralVisibility);

		ProgressBar aIndeterminateProgressBar = (ProgressBar) findViewById(R.id.progress_large);
		if (aIndeterminateProgressBar != null) {
			aIndeterminateProgressBar
					.setVisibility(RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService
							.getPlaybackStatus() == EPlaybackStatuses.CONNECTION_IN_PROGRESS ? View.VISIBLE
							: View.GONE);
		}
	}
}
